using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Database;
using DataObject.Core;

namespace DataObject.DataAccess
{
    public partial class ReservationDetailDAO : DataBaseProcessBase<ReservationDetail, OBSDataContext>
    {
        public static ReservationDetailDAO CreateInstant()
        {
            return new ReservationDetailDAO();
        }

        public List<ReservationDetail> GetReservationDetailBy(Guid reservationID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ReservationDetails.Where(x => x.ReservationID == reservationID).ToList();
            }
        }

        #region Extensibility Method Declarations
        public void InsertCollection(List<ReservationDetail> ls)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.ReservationDetails.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }
        public void Update(ReservationDetail item)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.UpdateReservationDetail(item.RDID, item.ReservationID, item.CabinID, item.Representative, item.NumOfPeople, item.CabinPrice, item.Status, item.Note);
            }
        }
        public List<ReservationDetail> GetReservationDetailAll()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ReservationDetails.ToList();
            }
        }
        public ReservationDetail GetReservationDetailById(System.Guid pRDID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ReservationDetails.SingleOrDefault(a => a.RDID == pRDID);
            }
        }
        public List<ReservationDetail> SelectListItem(System.Guid? RDID, System.Guid? ReservationID, int? CabinID, string Representative, int? NumOfPeople, decimal? CabinPrice, int? Status, string Note, HashSet<string> SortClause)
        {
            int Count = 0;
            return SelectListItem(RDID, ReservationID, CabinID, Representative, NumOfPeople, CabinPrice, Status, Note, null, null, out Count, SortClause);
        }
        public List<ReservationDetail> SelectListItem(System.Guid? RDID, System.Guid? ReservationID, int? CabinID, string Representative, int? NumOfPeople, decimal? CabinPrice, int? Status, string Note, int? Page, int? Row, out int Count, HashSet<string> SortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.ReservationDetails select table);
                if (RDID.HasValue)
                    query = query.Where(table => table.RDID == RDID.Value);
                if (ReservationID.HasValue)
                    query = query.Where(table => table.ReservationID == ReservationID.Value);
                if (CabinID.HasValue)
                    query = query.Where(table => table.CabinID == CabinID.Value);
                if (!Representative.Equals(string.Empty))
                    query = query.Where(table => table.Representative.ToLower().Equals(Representative.ToLower()));
                if (NumOfPeople.HasValue)
                    query = query.Where(table => table.NumOfPeople == NumOfPeople.Value);
                if (CabinPrice.HasValue)
                    query = query.Where(table => table.CabinPrice == CabinPrice.Value);
                if (Status.HasValue)
                    query = query.Where(table => table.Status == Status.Value);
                if (!Note.Equals(string.Empty))
                    query = query.Where(table => table.Note.ToLower().Equals(Note.ToLower()));
                for (int i = SortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(SortClause.ElementAt(i));
                Count = query.Count();
                if (Page.HasValue && Row.HasValue)
                    return query.PageIQuery(Page.Value, Row.Value).ToList();
                return query.ToList();
            }
        }
        #endregion
    }    
}
